﻿--[[
   @brief    tools_panel 
   @author   hqzheng
   @version  1.0
   @date     2017-04-21
--]]

tools_panel_gift_actions = {
    kGiftButtonShow = "show",
    kGiftButtonClick = "click"
};

tools_panel = 
{
    mainframe_dialog_id = 0;
    panel_id = 0;
    self_info_inner_btn_id = 0;
    self_info_btn_id = 0;
    self_info_text_id = 0;
    msg_box_btn_id = 0;
    chat_btn_id = 0;
    msg_box_red_point_id = 0;
    chat_msg_red_point_id = 0;
    self_head_ctn_id = 0;
    gift_btn_id = 0;
    download_mgr_item_id = 0;
    download_mgr_btn_id = 0;
    gift_red_point_id = 0;
    gift_btn_url = "";
    MENU_OFFSET_X = 15;
    MENU_OFFSET_Y = 34;
    MAX_NAME_WIDTH = 70;
    USER_INFO_BTN_OFFSET_X = 40;
    SELF_MENU_WIDTH = 150;
    ITEM_WIDTH = 50;

    btn_info = {};
    plugin_name_2_item_id = {};
    btn_ctn_ptr = nil;
    has_init_social = false;
    has_downloading = false;
    msg_red_point_visible = false;

    kUrlToolPanelConfig = LoadStr("GL_GAME_CONFIG_URL");

    switch_info = nil;
    panel_width = 0;
    real_itme_count = 0;
    shopping_cart_num = 0;
    init = function (parent_id, mainframe_dialog_id)
        tools_panel.get_switch_info();
        ierd_tgp.tpf_ui.Require("mainframe_tools_panel\\self_menu.lua");
        ierd_tgp.tpf_ui.Require("mainframe_tools_panel\\download_mgr\\control.lua");
        ierd_tgp.tpf_ui.Require("mainframe_tools_panel\\verify_tips.lua");
        tools_panel.mainframe_dialog_id = mainframe_dialog_id;
        
        local plugin_info_path = "mainframe_tools_panel\\lua_plugins_info.xml";

        local node_count = lua_plugin_mgr.add_module_plugins("tools_panel", plugin_info_path);
        tools_panel.panel_width = node_count * tools_panel.ITEM_WIDTH;
        ui_utility.setproperty_by_id(parent_id, "Width", tools_panel.panel_width);
        ui_utility.setproperty_by_id(parent_id, "ColumnCount", node_count);

        tools_panel.btn_ctn_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
        if nil ~= tools_panel.btn_ctn_ptr then
            tools_panel.btn_ctn_ptr:ClearAllItems();
        end
        lua_plugin_mgr.load_plugins("tools_panel");

        local item_id = tools_panel.plugin_name_2_item_id["self_head"];
        login_verify_tips.init(mainframe_dialog_id);
        login_verify_tips.set_reference_control(item_id);

        event_center.add_listener(group_event.SHOW_MSG_BOX_NOTIFY_REDDOT, "tools_panel.show_msg_box_notify_reddot");
        event_center.add_listener(group_event.CLEAR_MSG_BOX_NOTIFY_REDDOT, "tools_panel.clear_msg_box_notify_reddot");
        if base_utility.get_client_version_type() == base_utility.CLIENT_TYPE.CHINA_VER then
            event_center.add_listener(group_event.PWG_SHOW_SOCIAL_PANEL_RED_POINT, "tools_panel.show_chat_msg_reddot");
            event_center.add_listener(group_event.PWG_CLEAR_SOCIAL_PANEL_RED_POINT, "tools_panel.clear_chat_msg_reddot");
        else
            event_center.add_listener(group_event.SHOW_FRIEND_TAB_RED_POINT, "tools_panel.show_chat_msg_reddot");
        end
        comm_center.listen_broadcast("xxxx", tools_panel.on_downloading_status);  
        comm_center.listen_broadcast("Msg_ShoppingCartCountUpdate", tools_panel.on_shopping_cart_count_update);

        comm_center.listen_broadcast("i_brdcst_self_head_redpoint_update",
                                      tools_panel.on_self_head_red_point_update);
        comm_center.listen_broadcast("Msg_NotifySvrExceptionStateChange",
                                     "tools_panel.on_server_except_change");
        tools_panel.request_tools_panel_config();
        tools_panel.get_self_info();
        tools_panel.get_shopping_cart_count();
        login_verify_tips.request_bind_tips_config();
    end;

    show = function (data)
        if nil == data then
           return;
        end
        tools_panel.panel_id = data.tools_panel_id;
        tools_panel.init(data.tools_panel_id, data.dialog_id);
        ui_utility.set_visible(tools_panel.panel_id, true);
    end;

    get_self_info = function ()
        local user_game_info = ierd_tgp.users_info.User_game_info:new_local();
        local users_info_adapter =
            base_utility.get_component_adapter("IUsers_info_module",
                "ierd_tgp.users_info.GetUsersInfoModuleAdapter();");
        if nil ~= users_info_adapter then
            users_info_adapter:get_self_game_info(user_game_info);
            wegame_mainframe_model.self_head_url = user_game_info.head_url;
            tools_panel.set_self_head(wegame_mainframe_model.self_head_url);
            wegame_mainframe_model.self_tgp_id = user_game_info.ullTGPID;
            wegame_mainframe_model.self_uin = user_game_info.uin;
            wegame_mainframe_model.get_big_v_info();
        end
    end;

    on_handle_self_info_update = function (data)
        base_utility.log("[tools_panel]on_handle_self_info_update");
        if "" ~= data.head_url then
            wegame_mainframe_model.self_head_url = data.head_url;
            tools_panel.set_self_head(wegame_mainframe_model.self_head_url);
        end

        if nil == data.tgp_id or "" == data.tgp_id then
            return;
        end

        wegame_mainframe_model.self_tgp_id = data.tgp_id;
        wegame_mainframe_model.self_uin = tonumber(data.qq_id);
        wegame_mainframe_model.get_big_v_info();
        if false == tools_panel.has_init_social then
            -- 如果社交已经配置，则启初始化社交
            comm_center.call_service("i_service_social_init", {});
            tools_panel.has_init_social = true;
        end
    end;

    set_self_head = function (head_url)
        local item_id = tools_panel.plugin_name_2_item_id["self_head"];
        if nil ~= item_id then
            tools_panel.set_img_bkg(item_id, head_url);
        end
    end;

    on_user_info_click = function (uilib, control_id)
        base_utility.mainframe_report("main_window", "mainframe_btn_click", "head");
        -- 显示菜单
        local menu_id = ui_utility.get_ctrl_id_by_path(tools_panel.mainframe_dialog_id, "mnShop");
        local btn_tab_ptr = ui_utility.get_ctrl_ptr_by_id(control_id);
        local mainframe_ptr = ui_utility.get_ctrl_ptr_by_id(tools_panel.mainframe_dialog_id);
        if nil == btn_tab_ptr or nil == mainframe_ptr then
            return;
        end
        local btn_tab_rc = btn_tab_ptr:GetRectEx(TRUE,TRUE);
        local mainframe_rc = mainframe_ptr:GetRectEx(TRUE,TRUE);
        local btn_tab_width = btn_tab_rc.right - btn_tab_rc.left;
        local offset_x = btn_tab_rc.left - mainframe_rc.left - (tools_panel.SELF_MENU_WIDTH - btn_tab_width) / 2;
        local offset_y = btn_tab_rc.top - mainframe_rc.top + tools_panel.MENU_OFFSET_Y;
        self_menu.show(menu_id, true, offset_x, offset_y);
        self_menu.set_msgbox_redpoint_visible(tools_panel.msg_red_point_visible);
    end;

    on_msg_box_btn_click = function (uilib, control_id)
        base_utility.mainframe_report("main_window", "mainframe_btn_click", "msgbox");
        event_center.send_event(group_event.SHOW_MESSAGE_BOX_DIALOG, {});
    end;

    -- 显示消息盒子的红点
    show_msg_box_notify_reddot = function (event_id, json_str)
        base_utility.log("[tools_panel]show_msg_box_notify_reddot");
--        local item_id = tools_panel.plugin_name_2_item_id["msg_box_frame"];
--        tools_panel.set_redpoint_visible(item_id, true);
        tools_panel.msg_red_point_visible = true;
        self_menu.set_msgbox_redpoint_visible(tools_panel.msg_red_point_visible);
    end;

    -- 清除消息盒子的红点
    clear_msg_box_notify_reddot = function (event_id, json_str)
        base_utility.log("[tools_panel]clear_msg_box_notify_reddot");
--        local item_id = tools_panel.plugin_name_2_item_id["msg_box_frame"];
--        tools_panel.set_redpoint_visible(item_id, false);
        tools_panel.msg_red_point_visible = false;
        self_menu.set_msgbox_redpoint_visible(tools_panel.msg_red_point_visible);
    end;

    -- 消息社交入口聊天消息红点
    show_chat_msg_reddot = function (event_id, json_str)
        local message = base_utility.json_to_table(json_str);
        if message == nil or message.show == nil then
            return;
        end

        local data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
        if data.is_show == 0 then
            -- 社交面板没有显示，显示红点
            local item_id = tools_panel.plugin_name_2_item_id["social"];
            if message.show == "true" then
                base_utility.log("[tools_panel]show_msg_box_notify_reddot");
                tools_panel.set_redpoint_visible(item_id, true);
            else
                tools_panel.set_redpoint_visible(item_id, false);
            end
        end
    end;

    -- 清楚社交入口聊天消息红点
    clear_chat_msg_reddot = function (event_id, json_str)
        base_utility.log("[tools_panel]clear_chat_msg_reddot");
        local item_id = tools_panel.plugin_name_2_item_id["social"];
        tools_panel.set_redpoint_visible(item_id, false);
    end;

    -- 显示下载管理器的红点
    show_download_mgr_notify_reddot = function (data_in)
        base_utility.log("[tools_panel]show_download_mgr_notify_reddot");
        local item_id = tools_panel.plugin_name_2_item_id["download_mgr"];
        tools_panel.set_redpoint_visible(item_id, true);
    end;

    -- 清除下载管理器的红点
    clear_download_mgr_notify_reddot = function (data_in)
        base_utility.log("[tools_panel]clear_download_mgr_notify_reddot");
        local item_id = tools_panel.plugin_name_2_item_id["download_mgr"];
        tools_panel.set_redpoint_visible(item_id, false);
    end;

    on_chat_btn_click = function (uilib, control_id)
        if false == tools_panel.has_init_social then
            base_utility.log("[tools_panel]social not init yet");
            return;
        end
        base_utility.mainframe_report("main_window", "mainframe_btn_click", "social");
        local is_close_social = comm_center.call_service("Svr_SocialClosed", {});
        if nil ~= is_close_social and 1 == is_close_social.is_closed then
            ui_utility.setproperty_by_id(tools_panel.chat_btn_id, "TextTip", LoadStr("SOCIAL_NETWORK_CHART_BTN_ENABLE_TIPS"));
        else
            tools_panel.handle_open_social_panel();
            tools_panel.clear_chat_msg_reddot();
            comm_center.send_broadcast("i_broadcast_social_title_mainbtn_clicked", {});
        end
        if false == tools_panel.has_downloading then
            tools_panel.set_downloading_ani(true);
            tools_panel.has_downloading = true;
        else
            tools_panel.set_downloading_ani(false);
            tools_panel.has_downloading = false;
        end
    end;

    handle_open_social_panel = function (args)
        local event_param = {};
        event_param.show = 1;
        event_center.send_event(mainframe_event.OPEN_SOCIAL_PANEL, 
                                base_utility.table_to_json(event_param));
    end;

    on_mouse_hover = function (uilib, control_id)
        base_utility.log("[tools_panel]on_mouse_hover");

        -- 显示菜单
        local menu_id = ui_utility.get_ctrl_id_by_path(tools_panel.mainframe_dialog_id, "mnShop");
        local btn_tab_ptr = ui_utility.get_ctrl_ptr_by_id(control_id);
        local mainframe_ptr = ui_utility.get_ctrl_ptr_by_id(tools_panel.mainframe_dialog_id);
        if nil == btn_tab_ptr or nil == mainframe_ptr then
            return;
        end
        local btn_tab_rc = btn_tab_ptr:GetRectEx(TRUE,TRUE);
        local mainframe_rc = mainframe_ptr:GetRectEx(TRUE,TRUE);
        local btn_tab_width = btn_tab_rc.right - btn_tab_rc.left;
        local offset_x = btn_tab_rc.left - mainframe_rc.left - (tools_panel.SELF_MENU_WIDTH - btn_tab_width) / 2;
        local offset_y = btn_tab_rc.top - mainframe_rc.top + tools_panel.MENU_OFFSET_Y;
        self_menu.show(menu_id, true, offset_x, offset_y);
    end;

    on_mouse_leave = function (uilib, control_id)
        local tpfui = base_utility.get_component_adapter("ITpf_ui",
                        "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil ~= tpfui then
            tpfui:RemoveInterval("tools_panel.delay_handle_leave_msg");
            tpfui:AddInterval("tools_panel.delay_handle_leave_msg", 200, control_id);
            base_utility.log("[tools_panel]on_mouse_leave");
        end
    end; 

    delay_handle_leave_msg = function (control_id)
        if true == tools_panel.is_mouse_in_menu_and_btn() then
            return;
        end

        local tpfui = base_utility.get_component_adapter("ITpf_ui",
                        "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil ~= tpfui then
            tpfui:RemoveInterval("tools_panel.delay_handle_leave_msg", control_id);
        end
        -- 隐藏菜单
        local menu_id = ui_utility.get_ctrl_id_by_path(tools_panel.mainframe_dialog_id, "mnShop");
        ui_utility.setproperty_by_id(menu_id, "Visible", "false");
    end;

    is_mouse_in_menu_and_btn = function ()
        local menu_id = ui_utility.get_ctrl_id_by_path(tools_panel.mainframe_dialog_id, "mnShop");
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(menu_id);
        local inner_btn_ptr = ui_utility.get_ctrl_ptr_by_id(tools_panel.self_info_btn_id);
        if nil == menu_ptr or nil == inner_btn_ptr then
            return false;
        end

        local rect_menu = menu_ptr:GetRectEx(TRUE,TRUE); 
        local point = ierd_tgp.tray_ui.Point:new_local();
        local tray_ui =
            base_utility.get_component_adapter("ITray_ui",
                "ierd_tgp.tray_ui.get_Tray_ui_adapter();");
        if tray_ui ~= nil then
            tray_ui:get_cursor_pos(point);
        end             
        local mouse_in = false;
        if rect_menu.left <= point.x and point.x <= rect_menu.right and
            rect_menu.top <= point.y and point.y <= rect_menu.bottom then
            mouse_in = true;
        end

        if not mouse_in then
            -- 判断是否在btn里
            local rect_btn = inner_btn_ptr:GetRectEx(TRUE,TRUE);
            if rect_btn.left <= point.x and point.x <= rect_btn.right and
                rect_btn.top <= point.y and point.y <= rect_btn.bottom then
                mouse_in = true;
            end
        end
        return mouse_in;
    end;

    proc_ui_node = function (ui_info_path)
        if nil == tools_panel.btn_ctn_ptr then
            return;
        end
        local xml = newParser();
        local xmlparser = xml:loadVfsFile(ui_info_path);
        if nil == xmlparser or nil == xmlparser.ui_info then
            return false;
        end
        local node_name = xmlparser.ui_info["@name"];
        local need_switch = false;
        if nil ~= xmlparser.ui_info.need_switch and 
            "true" == xmlparser.ui_info.need_switch["@name"] then
            need_switch = true;
        end
        if true == need_switch and false == tools_panel.get_plugin_switch(node_name) then
            return false;
        end
        -- 暂时屏蔽海外版的下载管理器入口
        if node_name == "download_mgr" then
            -- 下载管理器灰度入口
            if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
                --base_utility.log("[tools_panel]download_mgr donot display for oversea ver");
                --return;
            end
            --[[
            local param_data = comm_center.call_service("Svr_IsOpenGameDownloadMgr", nil);
            if param_data~= nil and param_data.is_open == false then
                base_utility.log("[tools_panel]tools panel download_mgr btn not load");
                
            end;
            ]]
        end;
        
        if node_name == "social" then
            if base_utility.is_type_of_oversea_ver() then
                return;
            end
        end

        local item_ctrl_id = tools_panel.btn_ctn_ptr:AddOneItem();
        local ui_info = {};
        ui_info["plugin_name"] = xmlparser.ui_info["@name"];
        tools_panel.plugin_name_2_item_id[ui_info.plugin_name] = item_ctrl_id;

        if nil ~= xmlparser.ui_info.background then
            ui_info["background"] = xmlparser.ui_info.background["@name"];
        end
        if nil ~= xmlparser.ui_info.background_hover then
            ui_info["background_hover"] = xmlparser.ui_info.background_hover["@name"];
        end
        if nil ~= xmlparser.ui_info.background_pressed then
            ui_info["background_pressed"] = xmlparser.ui_info.background_pressed["@name"];
        end
        if nil ~= xmlparser.ui_info.background_disable then
            ui_info["background_disable"] = xmlparser.ui_info.background_disable["@name"];
        end
        if nil ~= xmlparser.ui_info.tool_tip then
            ui_info["tool_tip"] = xmlparser.ui_info.tool_tip["@name"];
        end
        tools_panel.real_itme_count = tools_panel.real_itme_count + 1;
        tools_panel.panel_width = tools_panel.real_itme_count * tools_panel.ITEM_WIDTH;
        ui_utility.setproperty_by_id(parent_id, "Width", tools_panel.panel_width);

        tools_panel.btn_info[item_ctrl_id] = ui_info;
        tools_panel.set_btn_property(item_ctrl_id);
    end;

    set_btn_property = function (item_id)
        local btn_id = ui_utility.get_ctrl_id_by_path(item_id, "InerBtn");
        local btn_info = tools_panel.btn_info[item_id];
        if nil ~= btn_info then
            ui_utility.setproperty_by_id(btn_id, "Background", btn_info.background);
            ui_utility.setproperty_by_id(btn_id, "BackgroundHover", btn_info.background_hover);
            ui_utility.setproperty_by_id(btn_id, "BackgroundPressed", btn_info.background_pressed);
            ui_utility.setproperty_by_id(btn_id, "BackgroundDisable", btn_info.background_disable);
            if nil ~= btn_info.tool_tip then
                ui_utility.setproperty_by_id(btn_id, "TextTip_LID", btn_info.tool_tip);
            end
            ui_utility.setproperty_by_id(btn_id, "OnClick", "tools_panel.on_click_btn");
            if "self_head" == btn_info.plugin_name then
                tools_panel.self_info_btn_id = btn_id;
            elseif "downloading_status" == btn_info.plugin_name then
                ui_utility.setproperty_by_id(btn_id, "Disable", "true");
            elseif "download_mgr" == btn_info.plugin_name then
                --[[
                local show = false;
                local param_data = comm_center.call_service("Svr_IsOpenGameDownloadMgr", nil);
                if param_data~= nil and param_data.is_open == true then
                    show = true;
                end;
                if show then
                    ui_utility.setproperty_by_id(btn_id, "Visible", "true");
                else
                    ui_utility.setproperty_by_id(btn_id, "Visible", "false");
                     -- TODO 位置移动
                end;
                ]]
                ui_utility.setproperty_by_id(btn_id, "Visible", "true");
                tools_panel.download_mgr_btn_id = btn_id;
                tools_panel.download_mgr_item_id = item_id;
            elseif "gift" == btn_info.plugin_name then
                -- ui_utility.setproperty_by_id(btn_id, "Visible", "false");
                tools_panel.gift_btn_id = btn_id;
                tools_panel.set_gift_btn_visible(false);
            elseif "social" == btn_info.plugin_name then
                tools_panel.chat_btn_id = btn_id;
                tools_panel.chat_item_id = item_id;
                local query_result = comm_center.call_service("Srv_QueryExceptionMode", {});
                if nil ~= query_result and true == query_result.is_svr_exception_mode then
                    ui_utility.setproperty_by_id(btn_id, "Disable", "true");
                end
                local social_state = comm_center.call_service("i_service_check_pwg_social_state",
                                                              {});
                if nil ~= social_state and social_state.is_enabled then
                    ui_utility.setproperty_by_id(btn_id, "TextTip_LID", "PWG_SOCIAL_CAPTION");
                end
            end
        end
    end;

    set_img_bkg = function (item_id, img_url)
        local inner_img_id = ui_utility.get_ctrl_id_by_path(item_id, "InnerImg");
        local url = img_url
        if nil ~= url and "" ~= url then
            url = "ONEIMAGE:client_mainframe\\info_default.png|" .. url .. ";Circle;0;";            
        else
            url = "ONEIMAGE:client_mainframe\\info_default.png;Circle;0";
        end;
        base_utility.log(string.format("[tools_panel][set_img_bkg]url:%s", url), true);
        ui_utility.setproperty_by_id(inner_img_id, "Background", url);
    end;

    set_redpoint_visible = function (item_id, visible)
        local redpoint_id = ui_utility.get_ctrl_id_by_path(item_id, "CtnRedPoint");
        if visible then
            ui_utility.setproperty_by_id(redpoint_id, "Visible", "true");
        else
            ui_utility.setproperty_by_id(redpoint_id, "Visible", "false");
        end
    end;

    set_redpoint_num_visible = function (item_id, visible, num)
        local redpoint_id = ui_utility.get_ctrl_id_by_path(item_id, "CtnRedNum");
        local redpoint_big_id = ui_utility.get_ctrl_id_by_path(item_id, "CtnRedNumBig");
        local num_id = ui_utility.get_ctrl_id_by_path(item_id, "CtnRedNum\\num");
        local num_big_id = ui_utility.get_ctrl_id_by_path(item_id, "CtnRedNumBig\\num");
        if visible then
            if num > 0 and num < 10 then
                ui_utility.setproperty_by_id(redpoint_id, "Visible", "true");
                ui_utility.setproperty_by_id(redpoint_big_id, "Visible", "false");
                ui_utility.setproperty_by_id(num_id, "Visible", "false");
                ui_utility.setproperty_by_id(redpoint_id, "Background",
                    "NONE");
                ui_utility.setproperty_by_id(redpoint_id, "Background",
                    "ONEIMAGE:client_mainframe\\top_title\\redpoint_ani\\small.tfc;Stretch;0;");
                ui_utility.setproperty_by_id(num_id, "Caption", tostring(num));
            else
                if num >= 100 then
                    num = "···";
                end
                ui_utility.setproperty_by_id(redpoint_id, "Visible", "false");
                ui_utility.setproperty_by_id(redpoint_big_id, "Visible", "true");
                ui_utility.setproperty_by_id(num_id, "Visible", "false");
                ui_utility.setproperty_by_id(redpoint_big_id, "Background",
                    "NONE");
                ui_utility.setproperty_by_id(redpoint_big_id, "Background",
                    "ONEIMAGE:client_mainframe\\top_title\\redpoint_ani\\big.tfc;Stretch;0;");
                ui_utility.setproperty_by_id(num_big_id, "Caption", tostring(num));
            end

        else
            ui_utility.setproperty_by_id(redpoint_id, "Visible", "false");
            ui_utility.setproperty_by_id(redpoint_big_id, "Visible", "false");
        end
    end;

    on_small_ani_stop = function (uilib, control_id)
       local item_id = tools_panel.plugin_name_2_item_id["shopping_cart"];
       local num_id = ui_utility.get_ctrl_id_by_path(item_id, "CtnRedNum\\num");
       ui_utility.setproperty_by_id(num_id, "Visible", "true");
    end;

    on_big_ani_stop = function (uilib, control_id)
       local item_id = tools_panel.plugin_name_2_item_id["shopping_cart"];
       local num_big_id = ui_utility.get_ctrl_id_by_path(item_id, "CtnRedNumBig\\num");
       ui_utility.setproperty_by_id(num_big_id, "Visible", "true");
    end;

    on_click_btn = function (uilib, control_id)
        local item_id = ui_utility.get_ctrl_parent_id(control_id);
        local btn_info = tools_panel.btn_info[item_id];
        if nil == btn_info then
            return;
        end

        if "self_head" == btn_info.plugin_name then
            tools_panel.on_user_info_click(uilib, control_id);
        elseif "social" == btn_info.plugin_name then
            tools_panel.on_chat_btn_click(uilib, control_id);
        elseif "msg_box_frame" == btn_info.plugin_name then
            tools_panel.on_msg_box_btn_click(uilib, control_id);
        elseif "downloading_status" == btn_info.plugin_name then
            tools_panel.on_downloading_status_btn_click(uilib, control_id);
        elseif "gift" == btn_info.plugin_name then
            tools_panel.on_gift_btn_click(uilib, control_id);
        elseif "download_mgr" == btn_info.plugin_name then
            tools_panel.on_download_mgr_btn_click(uilib, control_id);
        elseif "shopping_cart" == btn_info.plugin_name then
            tools_panel.on_shopping_cart_btn_click(uilib, control_id);
        end
    end;

    on_downloading_status_btn_click = function (uilib, control_id)
         base_utility.mainframe_report("main_window", "mainframe_btn_click", "downloading_status");
         if true == tools_panel.has_downloading then
             local get_game_id = comm_center.call_service("Srv_GetDownloadingOrUpdatringGame", {});
             if nil ~= get_game_id and 0 ~= get_game_id.game_id then
                 local req_data = {game_id = get_game_id.game_id};
                 comm_center.call_service("i_svr_focus_item", req_data);
             end
         end
    end;

    on_download_mgr_btn_click = function (uilib, control_id)
         base_utility.mainframe_report("main_window", "mainframe_btn_click", "download_mgr");
         tools_panel.clear_download_mgr_notify_reddot(nil);
         -- open download_mgr
         local req_data = {};
         comm_center.call_service("i_svr_show_download_mgr", req_data);
         -- TODO data report
    end;

    on_shopping_cart_btn_click = function (uilib, control_id)
        -- test
--        local item_id = tools_panel.plugin_name_2_item_id["shopping_cart"];
--        tools_panel.shopping_cart_num = tools_panel.shopping_cart_num + 1;
--        tools_panel.set_redpoint_num_visible(item_id, true, tools_panel.shopping_cart_num);

        base_utility.mainframe_report("main_window", "mainframe_btn_click", "shoppping_cart");
        local param = {};
        param.type = "jump_url";
        param.id = GAME_ID.GAME_ID_SHOP;
        param.url = LoadStr("SHOPPING_CART_URL");
        param.op_from = 0;
        local json_str = base_utility.table_to_json(param);
        web_utility.open_link(json_str);
    end;

    set_downloading_ani = function (is_start)
        local item_id = tools_panel.plugin_name_2_item_id["downloading_status"];
        if nil ~= item_id then
            local btn_id = ui_utility.get_ctrl_id_by_path(item_id, "InerBtn");
            local btn_info = tools_panel.btn_info[item_id];
            ui_utility.setproperty_by_id(btn_id, "Disable", "true");
            local img = "ONEIMAGE:client_mainframe\\download_mgr_normal.png;Stretch;0";
            if is_start then
                img = "ONEIMAGE:client_mainframe\\downloading.tfc;Stretch;0";
                ui_utility.setproperty_by_id(btn_id, "Disable", "false");
            end;
            if nil ~= btn_info then
                ui_utility.setproperty_by_id(btn_id, "Background", img);
                ui_utility.setproperty_by_id(btn_id, "BackgroundHover", img);
                ui_utility.setproperty_by_id(btn_id, "BackgroundPressed", img);
            end
        end
    end;

    on_downloading_status = function (data)
        if true == data.downloading then
            tools_panel.set_downloading_ani(true);
            tools_panel.has_downloading = true;
        else
            tools_panel.set_downloading_ani(false);
            tools_panel.has_downloading = false;
        end
    end;
    
    -- 顶栏游戏下载管理器按钮动画
    on_download_mgr_mode_change = function(data_in)
        if data_in ~= nil then
            local downloading = data_in.is_downloading;
            tools_panel.set_download_mgr_animation(downloading);
        end;
    end;

    on_download_mgr_switch_change = function(data_in)
        if data_in ~= nil and data_in.is_open == true then
            local btn_info = tools_panel.btn_info[tools_panel.download_mgr_item_id];
            if btn_info ~= nil then
                ui_utility.setproperty_by_id(
                tools_panel.download_mgr_btn_id, "Background", btn_info.background);
                ui_utility.setproperty_by_id(
                tools_panel.download_mgr_btn_id, "BackgroundHover", btn_info.background_hover);
                ui_utility.setproperty_by_id(
                tools_panel.download_mgr_btn_id, "BackgroundPressed", btn_info.background_pressed);
                ui_utility.setproperty_by_id(
                tools_panel.download_mgr_btn_id, "BackgroundDisable", btn_info.background_disable);
            end;
            ui_utility.setproperty_by_id(tools_panel.download_mgr_btn_id, "Visible", "true");
        end
    end;

    set_download_mgr_animation = function(is_downloading)
        local item_id = tools_panel.plugin_name_2_item_id["download_mgr"];
        if nil ~= item_id then
            local btn_id = ui_utility.get_ctrl_id_by_path(item_id, "InerBtn");
            local btn_info = tools_panel.btn_info[item_id];
            
            if is_downloading then
                img = "ONEIMAGE:client_mainframe\\top_title\\download_mgr_ani\\"
                    .."dl_mgr_downloading.tfc;Stretch;0";
                if nil ~= btn_info then
                    ui_utility.setproperty_by_id(btn_id, "Background", img);
                    ui_utility.setproperty_by_id(btn_id, "BackgroundHover", img);
                    ui_utility.setproperty_by_id(btn_id, "BackgroundPressed", img);
                end
            else
                local normal_img = 
                    "ONEIMAGE:client_mainframe\\top_title\\download_mgr_normal.png;Stretch;0";
                local hover_img = 
                    "ONEIMAGE:client_mainframe\\top_title\\download_mgr_hover.png;Stretch;0";
                local press_img = 
                    "ONEIMAGE:client_mainframe\\top_title\\download_mgr_press.png;Stretch;0";
                if nil ~= btn_info then
                    ui_utility.setproperty_by_id(btn_id, "Background", normal_img);
                    ui_utility.setproperty_by_id(btn_id, "BackgroundHover", hover_img);
                    ui_utility.setproperty_by_id(btn_id, "BackgroundPressed", press_img);
                end
            end;
        end
    end;

    on_gift_btn_click = function(uilib, control_id)
        if "" ~= tools_panel.gift_btn_url then
            if nil == tools_panel.gift_extern_width or nil == tools_panel.gift_extern_height then
                local param = { 
                    type = "open_web", 
                    browser = "new", 
                    use_ptlogin = 1, 
                    url = tools_panel.gift_btn_url
                };
                comm_center.call_service("i_service_open_link_confirm_launch", param);
            else
                local param = {
                    url = tools_panel.gift_btn_url;
                    use_ptlogin = true;
                    width = tools_panel.gift_extern_width;
                    height = tools_panel.gift_extern_height;
                    caption = LoadStr("TPF_BADGE_STORE");
                };
                comm_center.call_service("i_service_show_simple_web_window", param);
            end
            tools_panel.report_gift_operation(tools_panel_gift_actions.kGiftButtonClick);
        end

        local item_id = tools_panel.plugin_name_2_item_id["gift"];
        if nil ~= item_id then
            tools_panel.set_redpoint_visible(item_id, false);
        end
    end;

    get_local_gift_config = function()
        local data = {};
        data["common"] = 0;
        data["key"] = "tools_panel.gift";
        local config = comm_center.call_service("Svr_GetConfig", data);
        return config;
    end;

    set_local_gift_config = function()
        local data = {};
        data["common"] = 0;
        data["key"] = "tools_panel.gift";
        local config_tbl = {};
        config_tbl["last_url"] = tools_panel.gift_btn_url;
        data["value"] = base_utility.table_to_json(config_tbl);
        comm_center.call_service("Svr_SetConfig", data);
    end;

    request_tools_panel_config = function ()

        local client_type = 1;
        if base_utility.is_type_of_oversea_ver() then
            client_type = 2;
        end

        local post_data = {
            data_names = "tools_panel_config",
            command = "list_all",
            params = {
                start_page = 0,
                items_per_pager = 50
            },
            stamp = {
                credential = {
                    agent_client_type = client_type,
                },
                agent_client_language = base_utility.get_currrent_language_str();
            }
        };
        base_utility.curl_request_async(tools_panel.kUrlToolPanelConfig
                           , "tools_panel.on_request_tools_panel_config"
                   , false, nil, true, nil, base_utility.table_to_json(post_data));
    end;

    on_request_tools_panel_config = function(response)
        base_utility.log("[tools_panel]tools panel config ".. response);
        local res_tbl = base_utility.json_to_table(response);
        if nil == res_tbl or nil == res_tbl.result then
            return
        end
        if 0 ~= tonumber(res_tbl.result.error_code) then
            return
        end

        local items_count = 0;
        if nil ~= res_tbl.items and type(res_tbl.items) == "table" then
            items_count = #res_tbl.items;
        end

        if items_count < 1 then
            return
        end

        local cfg = res_tbl.items[1];
        if nil == cfg then
            return
        end
        
        -- url
        if nil ~= cfg.gift_url then
            tools_panel.gift_btn_url = cfg.gift_url;
        end

        --icon
        if nil ~= cfg.gift_btn_icon and "" ~= cfg.gift_btn_icon then
            local background_url = "ONEIMAGE:client_mainframe\\top_title\\Gift_Normal.png|"
                .. cfg.gift_btn_icon .. ";Stretch;0;";
            ui_utility.setproperty_by_id(tools_panel.gift_btn_id, "Background", background_url);
            ui_utility.setproperty_by_id(tools_panel.gift_btn_id, "BackgroundHover", background_url);
            ui_utility.setproperty_by_id(tools_panel.gift_btn_id, "BackgroundPressed", background_url);
            ui_utility.setproperty_by_id(tools_panel.gift_btn_id, "BackgroundDisable", background_url);
        end

        -- external web page size
        if nil ~= cfg.width and "" ~= cfg.width then
            tools_panel.gift_extern_width = tonumber(cfg.width);
        end

        if nil ~= cfg.height and "" ~= cfg.height then
            tools_panel.gift_extern_height = tonumber(cfg.height);
        end

        if 1 == tonumber(cfg.show_gift_btn) then
            -- ui_utility.setproperty_by_id(tools_panel.gift_btn_id, "Visible", "true");
            tools_panel.set_gift_btn_visible(true);
            -- red point
            local last_url = "";
            local cfg_local = tools_panel.get_local_gift_config();
            if nil ~= cfg_local and nil ~= cfg_local.value then
                local val_tbl = base_utility.json_to_table(cfg_local.value);
                if nil ~= val_tbl then
                    last_url = val_tbl.last_url or "";
                end
            end

            if last_url ~= tools_panel.gift_btn_url then
                local item_id = tools_panel.plugin_name_2_item_id["gift"];
                if nil ~= item_id then
                    tools_panel.set_redpoint_visible(item_id, true);
                end
                tools_panel.set_local_gift_config();
            end
            tools_panel.report_gift_operation(tools_panel_gift_actions.kGiftButtonShow);
        end
    end;

    report_gift_operation = function(action)
        local qos_data = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local kv_data = {};
        kv_data["action" ] = action;
        qos_data.oc_type    = 1;
        qos_data.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kGameUpdateLackSpace;
        qos_data.str_action = "wegame_app";
        qos_data.str_type   = "icon";
        qos_data.str_json   = base_utility.table_to_json(kv_data);
        base_utility.lua_qos_ex(qos_data);
        qos_data:delete();
    end;
    get_switch_info = function ()
        local req_data = {key = "mainframe_tools_switch"};
        local data = comm_center.call_service("i_svr_get_common_cfg", req_data);        
        if nil ~= data and data.value ~= nil and data.value ~= "" then
            tools_panel.switch_info = base_utility.json_to_table(data.value);
        else
            base_utility.log("mainframe_tools_switch", "query fail.")
        end;
    end;

    get_plugin_switch = function (plugin_name)
        local is_open = false;
        if nil == tools_panel.switch_info then
            return is_open;
        end

        for k, v in pairs(tools_panel.switch_info) do
            if v.plugin_name == plugin_name and true == v.is_open then
                is_open = true;
                break;
            end
        end
        return is_open;
    end;

    set_gift_btn_visible = function (is_visible)
        if false == is_visible then
            ui_utility.setproperty_by_id(tools_panel.gift_btn_id, "Visible", "false");
            tools_panel.panel_width = tools_panel.panel_width - tools_panel.ITEM_WIDTH;
            ui_utility.setproperty_by_id(tools_panel.panel_id, "Width", tostring(tools_panel.panel_width));
        else
            ui_utility.setproperty_by_id(tools_panel.gift_btn_id, "Visible", "true");
            tools_panel.panel_width = tools_panel.panel_width + tools_panel.ITEM_WIDTH;
            ui_utility.setproperty_by_id(tools_panel.panel_id, "Width", tostring(tools_panel.panel_width));
            local ptr = ui_utility.get_ctrl_ptr_by_id(tools_panel.panel_id);
            if nil ~= ptr then
                local rct = ptr:GetRect(FALSE);
                rct.left = rct.left - tools_panel.ITEM_WIDTH;
                rct.right = rct.right - tools_panel.ITEM_WIDTH;
                ptr:Move(rct);
            end
            -- update verify tips margins
            local item_id = tools_panel.plugin_name_2_item_id["self_head"];
            login_verify_tips.set_reference_control(item_id);
        end
    end;

    on_shopping_cart_count_update = function (data)
        if nil == data or nil == data.product_count then
            return;
        end
        local item_id = tools_panel.plugin_name_2_item_id["shopping_cart"];
        if data.product_count > 0 then
            tools_panel.set_redpoint_num_visible(item_id, true, data.product_count);
        else
            tools_panel.set_redpoint_num_visible(item_id, false, data.product_count);
        end
    end;

    get_shopping_cart_count = function ()
        comm_center.call_service("Svr_QueryShoppingCartCount", {});
    end;

    on_self_head_red_point_update = function(data)
        local red_point_count = 0;

        if tools_panel.msg_red_point_visible then
            red_point_count = red_point_count + 1;
        end

        local item_id = tools_panel.plugin_name_2_item_id["self_head"];

        if red_point_count > 0 then
            tools_panel.set_redpoint_visible(item_id, true);
        else
            tools_panel.set_redpoint_visible(item_id, false);
        end

    end;

    on_server_except_change = function(param)
        if nil == param or nil == param.is_svr_exception_mode then
            return;
        end
        if false == param.is_svr_exception_mode then
            if nil ~= tools_panel.chat_btn_id then
                ui_utility.setproperty_by_id(tools_panel.chat_btn_id, "Disable", "false");
            end
        end
    end;
};

